package top.kaoshanji.q1ch06d.aspects;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;

import java.util.Arrays;
import java.util.logging.Logger;

/**
 * @author kaoshanji
 * ^_^
 * create time 2025/4/29 10:41
 */
@Aspect
public class LoggingAspect {

    private Logger logger = Logger.getLogger(LoggingAspect.class.getName());

    /**
     * 代码清单-q1ch06d-127页c：更改切入点表达式，将切面编织到带注解的方法中
     * @param joinPoint ProceedingJoinPoint
     * @return
     * @throws Throwable
     */
    @Around("@annotation(ToLog)")
    public Object log(ProceedingJoinPoint joinPoint) throws Throwable {

        String methodName = joinPoint.getSignature().getName();
        Object [] arguments = joinPoint.getArgs();

        logger.info("...打印目标方法的名称：" + methodName + " ，参数：" + Arrays.asList(arguments));

        // 执行目标方法
        Object returnedByMethod = joinPoint.proceed();

        logger.info("...目标方法执行的结果：" + returnedByMethod);

        return returnedByMethod;

    }


}
